<?php

namespace app\system\controller\customer;

use app\common\controller\System;
use app\system\controller\daily\Throwable;
use hehe\Numeral;
use hehe\Str;
use hehe\Trade;
use think\facade\Db;


class OrderAgent extends System {
    /**
     * Goods模型对象
     * @var object
     */
    protected object $model;

    protected string|array $defaultSortField = 'id,desc';

    protected array|string $preExcludeFields = ['id', 'create_time'];

    protected string|array $quickSearchField = ['corp_name_cn', 'corp_name_ko', 'manager'];

    public function initialize(): void {
        parent::initialize();
        $this->model = new \app\system\model\customer\OrderAgent;
    }

    /**
     * 删除代理商
     */
    public function delAgent() {
        $param = $this->request->param();
        $result = db::name('customer_order_agent')->where(['id' => $param['id']])->delete();
        if($result){
            $this->success();
        }else{
            $this->error();
        }
    }



    /**
     * 查看
     * @throws Throwable
     */
    public function index(): void {
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->param('select')) {
            $this->select();
        }
        $this->withJoinTable = ['agent'];
        list($where, $alias, $limit, $order) = $this->queryBuilder();
        $res = $this->model
            ->field($this->indexField)
            ->withJoin($this->withJoinTable, $this->withJoinType)
            ->alias($alias)
            ->where($where);
        $param = $this->request->param();

        if(!empty($param['order_id'])){
            $res = $res->where(['order_id' => $param['order_id']]);
        }

        $res = $res->order($order)->paginate($limit);

        $list = $res->items();

        foreach($list as $key => $val){
            $list[$key]['profit'] = empty($val['profit']) ? null : Numeral::numberFormat($val['profit'] / 100);
            $list[$key]['commission'] = empty($val['commission']) ? null : Numeral::numberFormat($val['commission'] / 100);
            $list[$key]['to_commission'] = empty($val['to_commission']) ? null : Numeral::numberFormat($val['to_commission'] / 100);
            $list[$key]['commission_tax'] = empty($val['commission_tax']) ? null : Numeral::numberFormat($val['commission_tax'] / 100);
        }


        $this->success('', [
            'list' => $list,
            'total' => $res->total(),
            'remark' => get_route_remark(),
        ]);
    }



    /**
     * 删除代理商
     */
    public function del(array $ids = []): void {
        $id = $ids[0];
        db::startTrans();
        try{
            $row = db::name('customer_order_agent')->where(['id' => $id])->find();
            $order = db::name('customer_order')->where(['id' => $row['order_id']])->find();
            db::name('customer_order')->where(['id' => $order['id']])->update([
                'update_time' => $this->timestamp,
                'net_profit' => $order['net_profit'] + $row['commission']
            ]);
            $result = db::name('customer_order_agent')->where(['id' => $id])->delete();
            db::commit();
        }catch (\Exception $e){
            db::rollback();
            $this->error($e->getMessage());
        }

        if($result){
            $this->success();
        }else{
            $this->error();
        }
    }




























}